<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\ErrCode;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Support\ChangeSupport;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Relations\HasMany;

class OpenShopSpaceDesignSoftOutfitMappingModel extends BaseModel
{
    use KfSoftDeletes;

    protected $table = 'open_shop_space_design_soft_outfit_mapping';

    protected $fillable = [
        'orderNo',
        'preparationNo',
        'fileId',
        'fee',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
        'orderStatus',
        'payStatus',
        'payTime',
        'newOrderCode',
    ];

    protected $appends = [
        'orderStatusText',
        'payStatusText',
        'payTimeText'
    ];

    const ORDER_STATUS_0 = 0;
    const ORDER_STATUS_1 = 1;
    const ORDER_STATUS_2 = 2;
    const ORDER_STATUS_3 = 3;

    const CN_ORDER_STATUS = [
        self::ORDER_STATUS_0 => '已生成',
        self::ORDER_STATUS_1 => '进行中',
        self::ORDER_STATUS_2 => '已完成',
        self::ORDER_STATUS_3 => '已关闭',
    ];

    const ORDER_PAY_STATUS_0 = 0;
    const ORDER_PAY_STATUS_1 = 1;
    const ORDER_PAY_STATUS_2 = 2;
    const ORDER_PAY_STATUS_3 = 3;
    const ORDER_PAY_STATUS_4 = 4;

    const CN_PAY_STATUS = [
        self::ORDER_PAY_STATUS_0 => '未付款',
        self::ORDER_PAY_STATUS_1 => '部分支付',
        self::ORDER_PAY_STATUS_2 => '已支付',
        self::ORDER_PAY_STATUS_3 => '部分退款',
        self::ORDER_PAY_STATUS_4 => '全额退款',
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format("Y-m-d H:i:s");
    }

    public function getPayStatusTextAttribute()
    {
        return self::CN_PAY_STATUS[$this->payStatus];
    }

    public function getOrderStatusTextAttribute()
    {
        return self::CN_ORDER_STATUS[$this->orderStatus];
    }

    public function getPayTimeTextAttribute()
    {
        return $this->payTime != 0 ? date('Y-m-d H:i:s', $this->payTime) : '';
    }

    public function fileRel()
    {
        return $this->hasOne(OpenShopUploadFileResourceModel::class, 'id', 'fileId');
    }

    public function signOrderRel()
    {
        return $this->hasOne(OpenShopSignOrderModel::class, 'orderNo', 'orderNo');
    }

    /**
     * @return HasMany
     */
    public function payHistoryRel(): HasMany
    {
        return $this->hasMany(OpenShopSignOrderPayHistoryModel::class, "orderNo", "orderNo");
    }

    public function softLogisticsRel() :HasMany
    {
        return $this->hasMany(OpenShopSpaceDesignSoftLogisticsModel::class, "preparationNo", "preparationNo");
    }

    /**
     * 单笔支付成功
     * @param int $payPrice
     * @return $this
     */
    public function onePaySuccess(int $payPrice, ?int $fee = null): self
    {
        // 矫正应付金额
        if ($fee !== null) {
            $this->fee = $fee;
        }

        // 已支付金额累加
        $this->payFee += $payPrice;

        if ($this->payFee == $this->fee) {

            // 整单支付完成
            $this->payStatus   = self::ORDER_PAY_STATUS_2;
            $this->orderStatus = self::ORDER_STATUS_1;
            $this->payTime     = time();
        } elseif ($this->payFee < $this->fee) {

            // 部分支付完成
            $this->payStatus = self::ORDER_PAY_STATUS_1;
        } else {

            // 支付金额超过应付
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_PAY_PRICE_OVERFLOW)
            );
        }

        $this->save();

        return $this;
    }

    public static function getOrderLogTranslate()
    {
        return [
            'orderNo'       => '订单编号',
            'preparationNo' => '筹备订单号',
            'fileId'        => '附件id',
            'fee'           => [
                "type" => ChangeSupport::FILED_TYPE_PRICE,
                "desc" => "软装费"
            ],
            'createdAt'     => [
                "type" => ChangeSupport::FILED_TYPE_DATE_YMD_HIS,
                "desc" => "订单创建时间",
            ],
            'createdByUid'  => '创建人UID',
            'updatedAt'     => [
                "type" => ChangeSupport::FILED_TYPE_DATE_YMD_HIS,
                "desc" => "订单修改时间",
            ],
            'updatedByUid'  => '修改人UID',
        ];
    }
}
